package distriKub;

import java.io.IOException;
import java.net.DatagramSocket;
import java.net.ServerSocket;

public class ComRules {
	public static int chatPort = 9900;
	public static int gamePort = 9800;
	public static int serverPort = 9700;
	
	public static String comTerminationString = "***stop***";
	public static int standardTimeout = 2500; // MilliSeconds
	public static int extendedTimeout = 4000; // MilliSeconds
	public static int DBResponseTimeout = 7000; // MilliSeconds
	
	// A dictionary for parsing communication messages
	
	public static final String MessageDelimiter = "<--->";
	
	public static final String ChatMessage = "***ChatMessage***";
	public static final String BankUpdate = "***BankUpdate***";
	public static final String GameTimeSignatureUpdate = "***GameTimeSignatureUpdate***";
	public static final String AddOpponent = "***AddOpponent***";
	public static final String WinConfirmation = "***WinConfirmation***";
	public static final String TurnActivation = "***TurnActivation***";
	public static final String EndOfGame = "***EndOfGame***";
	public static final String CheckPulse = "***CheckPulse***";
	public static final String DeadPlayer = "***DeadPlayer***";
	public static final String YouAreDealer = "***YouAreDealer***";
	public static final String OpponentToPlay = "***OpponentToPlay***"; // An update of who's turn is it now
	public static final String RegisterNewPlayer = "***RegisterNewPlayer***";
	public static final String DBOKRegistered = "***DBOKRegistered***";
	public static final String DBERRORRegistered = "***DBERRORRegistered***";
	public static final String DBOK = "***DBOK***";
	public static final String DBError = "***DBError***";
	public static final String DBComError = "***DBComError***";
	public static final String RequestNameListFromServer = "***RequestNameListFromServer***";
	public static final String AreYouServer = "***AreYouServer***"; // This way the client asks a host whether it's a server
	public static final String RequestPlayersListInMyGroup = "***RequestPlayersListInMyGroup***";
	public static final String GameOverReport ="***GameOverReport***";
	
	// SERVER
	
	public static final String IWannaStart = "***IWannaStart***";
	public static final String BeginGame = "***BeginGame***";
	public static final String AddMeToGroup = "***AddMeToGroup***";
	public static final String OKAddedToGroup = "***OKAddedToGroup***";
	public static final String OKRegisteredResults = "***OKRegisteredResults***";
	public static final String LoginIncorrect = "***LoginIncorrect***";
	public static final String PlayerListInGroup = "***PlayerListInGroup***";
	public static final String NotEnoughPlayersToStartGame ="***NotEnoughPlayersToStartGame***";
	
	// End of dictionary for parsing communication messages
	
	
	// Assistant methods
	public static boolean portAvailable(int port) { // Checks whether a port number is available
		ServerSocket ss = null;
		DatagramSocket ds = null;
		try {
			ss = new ServerSocket(port);
			ss.setReuseAddress(true);
			ds = new DatagramSocket(port);
			ds.setReuseAddress(true);
			return true;
		} catch (IOException e) {
		} finally {
			if (ds != null) {
				ds.close();
			}

			if (ss != null) {
				try {
					ss.close();
				} catch (IOException e) {
					/* should not be thrown */
				} // End of catch block
			} // End of the last if block
		} // End of finally block

		return false;
	} // End of portAvailable method

	public static int findAvailablePort() {
		int desiredPort = 1025;
		while (!portAvailable(desiredPort))
			desiredPort++;
		return desiredPort;
	}
}
